package utilsEx

import (
	"errors"
	"io"
	"os"
	"path"
	"path/filepath"
	"strings"
	"time"
	ulog "utils/ulog"
)

// 判断所给路径文件/文件夹是否存在(返回true是存在)
func isExist(path string) bool {
	_, err := os.Stat(path) //os.Stat获取文件信息
	if err != nil {
		if os.IsExist(err) {
			return true
		}
		return false
	}
	return true
}
func createFile(filePath string) error {
	if !isExist(filePath) {
		err := os.MkdirAll(filePath, os.ModePerm)
		return err
	}
	return nil
}

//仅限于查询windows路径下的文件名，支持通配符
func findfiles(absolute_path string) (strls []string) {
	//仅限于查询windows路径下的文件名，支持通配符
	var base int = 0
	absolute_path = strings.ReplaceAll(absolute_path, "/", "\\")
	fileanmes := Cmdexec("dir " + absolute_path + " /s /b")
	arr := strings.Fields(fileanmes)

	if base == 0 {
		for i := 10; i < len(arr); i++ {
			if arr[i] == "/b" {
				base = i
				break
			}
		}
		base += 1
	}
	for {
		if base == 1 {
			fileanmes = Cmdexec("dir " + absolute_path + " /s /b")
			arr = strings.Fields(fileanmes)
			base = 0
			if base == 0 {
				for i := 10; i < len(arr); i++ {
					if arr[i] == "/b" {
						base = i
						break
					}
				}
				base += 1
			}
		} else {
			break
		}
	}
	//除去最后无用数据
	strls = make([]string, len(arr)-base-1)
	for i := base; i < len(arr)-1; i++ {
		strls[i-base] = arr[i]
	}
	if len(strls) == 1 && (strls[0] == "找不到文件" || strls[0] == "系统找不到指定的文件。") {
		strls = []string{}
	}
	return strls
}
func Move(oldpath, newpath string) error {
	dir, _ := path.Split(newpath)
	err := createFile(dir)
	if err != nil {
		return err
	}
	return _move(oldpath, newpath)
}
func Copy(oldpath, newpath string) error {
	if !isExist(oldpath) {
		return errors.New("can not find file!" + oldpath)
	}
	in, err := os.OpenFile(oldpath, os.O_RDWR|os.O_CREATE, 0666)
	if err != nil {
		return err
	}
	out, err := os.OpenFile(newpath, os.O_RDWR|os.O_CREATE, 0666)
	if err != nil {
		ulog.Logs.Warnln("to ", err)
	}
	_, err = io.Copy(out, in)
	if err != nil {
		return err
	}
	in.Close()
	out.Close()
	return nil
}

//paths 目录，此目录下文件全部清空
//diff_time 秒
func RemoveBefore(paths string, diff_time int64) error {
	now_time := time.Now().Unix() //当前时间，main使用Unix时间戳
	err := filepath.Walk(paths, func(paths string, f os.FileInfo, err error) error {
		if f == nil {
			return err
		}
		file_time := f.ModTime().Unix()
		if (now_time - file_time) > diff_time { //判断文件是否超过7天
			ulog.Logs.Printf("Delete file %v ", paths)
			dirs := isFile(paths)
			if dirs == true {
				os.RemoveAll(paths)
			}

		}
		return nil
	})
	if err != nil {
		return err
	} else {
		return nil
	}
}
func isFile(f string) bool {
	fi, e := os.Stat(f)
	if e != nil {
		return false
	}
	return !fi.IsDir()
}
